<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col :span="8">
                     <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query" clearable @clear="getOrderList">
                        <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <!--订单列表表格-->
             <el-table :data="orderList" style="width: 100%" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="order_number" label="订单编号"></el-table-column>
                <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
                <el-table-column label="是否付款" width="80">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.pay_status==='1'" type="success">已付款</el-tag>
                        <el-tag v-else type="danger">未付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
                <el-table-column label="下单时间" width="160">
                    <template slot-scope="scope">
                        {{scope.row.create_time | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="410">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="dialogEditShow(scope.row.order_id)">修改订单状态</el-button>
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="dialogAddressShow(scope.row.order_id)">修改收货地址</el-button>
                        <el-button type="success" icon="el-icon-location" size="mini" @click="dialogDeliveryShow('1106975712662')">查看物流详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5,8]"
    :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
        </el-card>
        <el-dialog title="修改订单信息" :visible.sync="dialogEditVisible" @close="dialogEditClose">
            <el-form :model="editOrder" ref="editOrderRef" label-width="100px">
                <el-form-item label="是否发货">
                    <el-radio-group v-model="editOrder.is_send">
                        <el-radio label="是">是</el-radio>
                        <el-radio label="否">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="订单支付">
                    <el-radio-group v-model="editOrder.order_pay">
                        <el-radio label="0">未支付</el-radio>
                        <el-radio label="1">支付宝</el-radio>
                        <el-radio label="2">微信</el-radio>
                        <el-radio label="3">银行卡</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="订单价格">
                    <el-input v-model="editOrder.order_price"></el-input>
                </el-form-item>
                <el-form-item label="订单数量">
                    <el-input v-model="editOrder.order_number"></el-input>
                </el-form-item>
                <el-form-item label="支付状态">
                    <el-radio-group v-model="editOrder.pay_status">
                        <el-radio label="1">是</el-radio>
                        <el-radio label="0">否</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogEditVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit(editOrder.order_id)">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="修改订单地址" :visible.sync="dialogAddressVisible" @close="dialogAddressClose">
            <el-form :model="newAddress" :rules="addressRules" ref="addressRef" label-width="100px">
                <el-form-item label="省市区/县" prop="address1">
                    <el-cascader :options="cityData" v-model="newAddress.address1"></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址" prop="address2">
                    <el-input v-model="newAddress.address2"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAddressVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit()">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="查看物流详情" :visible.sync="dialogDeliveryVisible">
            <el-timeline>
                <el-timeline-item v-for="(activity, i) in deliveryInfo" :key="i" :timestamp="activity.time">
                    {{activity.context}}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import cityData from './citydata'
export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: 1, // 当前的页数
                pagesize: 8// 当前每页显示多少条数据
            },
            orderList: [],
            total: 0,
            newAddress: {
                address1: [],
                address2: ''
            },
            addressRules: {
                address1: [
                    { required: true, message: '请选择省市区/县', trigger: 'blur' }
                ],
                address2: [
                    { required: true, message: '请输入详细地址', trigger: 'blur' }
                ]
            },
            editOrder: {},
            cityData,
            deliveryInfo: [],
            dialogEditVisible: false,
            dialogAddressVisible: false,
            dialogDeliveryVisible: false
        }
    },
    created() {
         this.getOrderList()
    },
    methods: {
         async getOrderList() {
            const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.orderList = res.data.goods
            this.total = res.data.total
        },
        handleSizeChange(newSize) {
            // 监听每页条数的改变
            this.queryInfo.pagesize = newSize
            this.getOrderList()
        },
        handleCurrentChange(newPage) {
            // 监听页码值改变
            this.queryInfo.pagenum = newPage
            this.getOrderList()
        },
        async dialogEditShow(id) { //
            const { data: res } = await this.$http.get(`/orders/${id}`)
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.editOrder = res.data
            this.dialogEditVisible = true
        },
        dialogAddressShow() { // 修改订单地址
            this.dialogAddressVisible = true
        },
        dialogEditClose() {
            this.$refs.editOrderRef.resetFields()
        },
        dialogAddressClose() {
            this.$refs.addressRef.resetFields()
        },
        async dialogDeliveryShow(id) {
            const { data: res } = await this.$http.get(`/kuaidi/${id}`)
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.deliveryInfo = res.data
            this.dialogDeliveryVisible = true
        },
        async handleEdit(id) {
            const { data: res } = await this.$http.put(`/orders/${id}`, this.editOrder)
            if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
            this.$message.success(res.meta.msg)
            this.dialogEditVisible = false
        }

    }
}
</script>

<style lang="less" scoped>
.el-cascader{
    width: 100%;
}
</style>